﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Labs.Entity
{
    public class Company : TaxEntity
    {
        LinkedList<Employee> employees;

        public Company(string name, string taxid)
            : base(name, taxid)
        {
            Console.WriteLine("Company ctor " + Name.ToUpper());
           employees = new LinkedList<Employee>();
           int[] intsary = new int[10];
        }

        public decimal AverageSalary
        {
            get
            {
                return employees.Average(p => p.Salary);
            }
        }

        public IEnumerable<Employee> Employees
        {
            get { return employees; }
        }


        public override string TaxIdRegex
        {
            get { return @"^\d\d-\d{7}$"; }
        }

        public void Hire(Employee newHire)
        {
            employees.AddLast(newHire);
        }

        /// <summary>
        /// Pay all the employees in this company.
        /// </summary>
        /// <returns>total net pay</returns>
        override public decimal Pay()
        {
            decimal totalNet = 0m;
            foreach (Employee e in employees)
            {
                totalNet += e.Pay();
            }

            return totalNet;
        }

        public static void Main()
        {
            try
            {
                Company comp = new Company("Strickland", "12-3456789");
                Employee emp = new Employee("Hank", "123-45-6789", 100);
                comp.Hire(emp);
                comp.Hire(new Employee("Peggy", "124-45-6789", 200));
                comp.Hire(new Employee("Luann", "125-45-6789", 70));
                comp.Hire(new Employee("Bill", "126-45-6789", 130));
                comp.Hire(new Employee("Bobby", "127-45-6789", 100));

                Console.WriteLine("Average salary {0:c}", comp.AverageSalary);
                Employee bill = comp.Employees.Single(e=>e.Name == "Bill");

                decimal executiveAverage = comp.Employees.
                    Where(e => e.Salary > 100).Average(e => e.Salary);
                IEnumerable<Employee> richones = comp.Employees.
                    Where(e => e.Salary > 100).OrderBy(e => e.Name);

            

                Payer pauline = new Payer();
                pauline.NoisyPay(emp);
                pauline.NoisyPay(comp);
                pauline.NoisyPay(new Intern());

                DayLaborer dl = new DayLaborer();
                pauline.NoisyPay(dl);

                Homeless hl = new Homeless();
                pauline.FlexPay(hl.Donate);
                pauline.FlexPay(dl.Pay);
                pauline.FlexPay(comp.Pay);
            }
            catch (Exception ex)
            {
              // Console.WriteLine(ex);
            }
            Console.WriteLine("this happens no matter what");
        }

    }
}
